FROM alpine:3.6

# study from  https://gitee.com/soft_xiang/docker-fastdfs-allinone/blob/master/Dockerfile#

RUN echo -e "\
https://mirrors.aliyun.com/alpine/v3.6/main\n\
https://mirrors.aliyun.com/alpine/v3.6/community\n\
">/etc/apk/repositories

ARG TZ="Asia/Shanghai"

# 配置启动脚本，在启动时中根据环境变量替换nginx端口、fastdfs端口
# 默认fastdfs端口
ENV TRACKER_PORT 22122
# 默认fastdfs storage server 端口
ENV STORAGE_PORT 23000
# 默认nginx端口
ENV NGINX_PORT 8888
# 启动模式 ALL 全部启动, storage 只启动存储+nginx, tracker 只启动调配器
ENV TRACKER_IP 127.0.0.1
ENV STORAGE_GROUP group1
ENV FDFS_MODE ALL

ENV TZ ${TZ}
ENV LIBFASTCOMMON_VERSION V1.0.43
ENV FASTDFS_VERSION V5.12
ENV FAST_NGINX_MODULE_VERSION V1.20
ENV TENGINE_VERSION 2.3.2
ENV NGINX_VERSION 1.19.0
ENV FASTDFS_BASE /fdfs
ENV HOME /root
ENV CONFIG "\
        --prefix=/etc/nginx \
        --sbin-path=/usr/local/nginx/sbin/nginx \
        --modules-path=/root/tengine-2.3.2/modules \
        --conf-path=/etc/nginx/nginx.conf \
        --error-log-path=/usr/local/nginx/logs/error.log \
        --http-log-path=/usr/local/nginx/logs/access.log \
        --pid-path=/var/run/nginx.pid \
        --lock-path=/var/run/nginx.lock \
        --user=root \
		"
#安装准备
RUN apk update \
    && apk add --no-cache  git gcc libc-dev make automake autoconf libtool pcre pcre-dev zlib zlib-dev openssl-dev wget vim \
	&& apk add bash tzdata \
    && ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime \
    && echo ${TZ} > /etc/timezone \
	&& rm -rf /var/cache/apk/*

# 指定fastdfs 5.11版本
#下载fastdfs.libfastcommon.nginx插件源码
RUN cd /root \
    && git clone https://gitee.com/fastdfs100/libfastcommon.git --depth 1 -b $LIBFASTCOMMON_VERSION \
    && git clone https://gitee.com/fastdfs100/fastdfs.git --depth 1 -b $FASTDFS_VERSION \
    #&& wget http://tengine.taobao.org/download/tengine-$TENGINE_VERSION.tar.gz \
    && wget http://file.sxycy.cn:88/vod/download/nginx/nginx-$NGINX_VERSION.tar.gz \
    && git clone https://gitee.com/fastdfs100/fastdfs-nginx-module.git --depth 1 -b $FAST_NGINX_MODULE_VERSION

 # 安装libfastcommon
RUN cd ${HOME}/libfastcommon/ \
    && ./make.sh  \
    && ./make.sh install

# 安装fastdfs
RUN cd ${HOME}/fastdfs/ \
    && ./make.sh \
    && ./make.sh install \
    && cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf \
    && cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf \
    && cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf \
    && cp ${HOME}/fastdfs/conf/http.conf /etc/fdfs/ \
    && cp ${HOME}/fastdfs/conf/mime.types /etc/fdfs/ \
    && sed -i "s|/home/yuqing/fastdfs|$FASTDFS_BASE/tracker|g" /etc/fdfs/tracker.conf \
    && sed -i "s|/home/yuqing/fastdfs|$FASTDFS_BASE/storage|g" /etc/fdfs/storage.conf \
    && sed -i "s|/home/yuqing/fastdfs|$FASTDFS_BASE/storage|g" /etc/fdfs/client.conf

# 切换fastdfs-nginx-module 版本 ,只能指定 85347be190dcfe9d46bd67176f84afaa32718142版本
#RUN cd ${HOME}/fastdfs-nginx-module/
#	&& git fetch --depth 2 origin tag $FAST_NGINX_MODULE_VERSION \
#	&& git checkout tag $FAST_NGINX_MODULE_VERSION

# 获取nginx源码
RUN cd ${HOME} \
    && tar -zxvf nginx-$NGINX_VERSION.tar.gz  \
    && cd nginx-$NGINX_VERSION/ \
    && ./configure --add-module=${HOME}/fastdfs-nginx-module/src/ $CONFIG \
    && make \
    && make install

## 获取tengine源码
#RUN cd ${HOME} \
#    && tar -zxvf tengine-$TENGINE_VERSION.tar.gz  \
#    && cd tengine-$TENGINE_VERSION/ $CONFIG \
#    && ./configure --add-module=${HOME}/fastdfs-nginx-module/src/ \
#    && make \
#    && make install

# 设置nginx和fastdfs联合环境，并配置nginx
RUN cp ${HOME}/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs \
    && sed -i "s|^store_path0.*$|store_path0 = $FASTDFS_BASE/storage|g" /etc/fdfs/mod_fastdfs.conf \
    && sed -i "s|^url_have_group_name.*$|url_have_group_name=true|g" /etc/fdfs/mod_fastdfs.conf \
    && cd ${HOME}/fastdfs/conf/ \
    && echo -e "\
    user root; \n\
    daemon off; \n\
    events {\n\
        worker_connections  4096;\n\
    }\n\
    error_log /dev/stderr; \n\
    #access_log /dev/stdout; \n\
    http {\n\
        include       mime.types;\n\
        default_type  application/octet-stream;\n\
        server {\n\
            listen 8888;\n\
            server_name localhost;\n\
            location ~ /group[0-9]/M00 {\n\
                ngx_fastdfs_module;\n\
            }\n\
        }\n\
    }">/etc/nginx/nginx.conf

# 清理文件
RUN rm -rf ${HOME}/*
RUN apk del gcc libc-dev make openssl-dev


# 创建启动脚本
RUN echo -e "#!/bin/bash \n\
if [ \"\$TRACKER_IP\" = \"127.0.0.1\" ]; then \n\
    TRACKER_IP=`ifconfig eth0|grep 'inet addr'|awk -F ":" '{print $2}'|awk '{print $1}'`; \n\
fi \n\
# tracker配置 \n\
mkdir -p $FASTDFS_BASE/storage/data $FASTDFS_BASE/tracker; \n\
sed -i \"s/port.*$/port=\$TRACKER_PORT/g\" /etc/fdfs/tracker.conf; \n\
\n\
# nginx 配置 \n\
sed -i \"s/listen .*$/listen \$NGINX_PORT;/g\" /etc/nginx/nginx.conf; \n\
\n\
# storage配置 \n\
#ln -s $FASTDFS_BASE/storage/data/ $FASTDFS_BASE/storage/data/M00; \n\n\
sed -i \"s/http.server_port.*$/http.server_port=\$NGINX_PORT/g\" /etc/fdfs/storage.conf; \n\n\
sed -i \"s/^port.*$/port=\$STORAGE_PORT/g\" /etc/fdfs/storage.conf; \n\
sed -i \"s/^tracker_server.*$/tracker_server=\$TRACKER_IP:\$TRACKER_PORT/g\" /etc/fdfs/storage.conf; \n\
\n\
sed -i \"s/^tracker_server.*$/tracker_server=\$TRACKER_IP:\$TRACKER_PORT/g\" /etc/fdfs/mod_fastdfs.conf; \n\n\
sed -i \"s/^storage_server_port.*$/storage_server_port=\$STORAGE_PORT/g\" /etc/fdfs/mod_fastdfs.conf; \n\n\
\n\
sed -i \"s/^tracker_server.*$/tracker_server=\$TRACKER_IP:\$TRACKER_PORT/g\" /etc/fdfs/client.conf; \n\
if [ \"\$FDFS_MODE\" = \"ALL\" ]; then \n\
    /etc/init.d/fdfs_trackerd start; \n\
    /etc/init.d/fdfs_storaged start; \n\
    /usr/local/nginx/sbin/nginx; \n\
    #ln -sf /dev/stdout $FASTDFS_BASE/storage/logs/storage.log \n\
    #ln -sf /dev/stdout $FASTDFS_BASE/tracker/logs/trackerd.log \n\
elif [ \"\$FDFS_MODE\" = \"storage\" ]; then \n\
    /etc/init.d/fdfs_storaged start; \n\
    /usr/local/nginx/sbin/nginx; \n\
    #ln -sf /dev/stdout $FASTDFS_BASE/storage/logs/storage.log \n\
elif [ \"\$FDFS_MODE\" = \"tracker\" ]; then\n\
    /etc/init.d/fdfs_trackerd start; \n\
    #ln -sf /dev/stdout $FASTDFS_BASE/tracker/logs/trackerd.log \n\
else  \n\
    exit 1 \n\
fi \n\
while true \n\
do sleep 60m \n\
done \n\
">/start.sh \
&& chmod u+x /start.sh

# 暴露端口。改为采用host网络，不需要单独暴露端口
# EXPOSE 80 22122 23000

ENTRYPOINT ["/bin/bash","/start.sh"]

#构建镜像
# sudo docker build -t fastdfs:5.12 .
# 启动 tracker
# sudo docker run -e "FDFS_MODE=tracker" -d --restart=always --privileged=true --name fastdfs-5.12-tracker -v /home/docker/fastdfs:/fdfs --net=host fastdfs:5.12
# 启动 storage
# sudo docker run -e "FDFS_MODE=storage" -e "TRACKER_IP=192.168.100.130" --restart=always -d --privileged=true --name fastdfs-5.12-storage -v /home/docker/fastdfs:/fdfs --net=host fastdfs:5.12
#说明:
#-v /usr/local/data/fastdfs:/var/local/fdfs 为文件目录映射，文件最终保存在宿主机/usr/local/data/fastdfs目录中
#-e "TRACKER_IP=宿主机ip" 为宿主机局域网ip
#-e "NGINX_PORT=8888" 自定义web端口，还支持TRACKER_PORT（实际为tracker_server的port，当初起名没起好，懒得改了）、STORAGE_PORT
#--net="host" 必须为host模式

# 删除 none 镜像 sudo docker rmi $(sudo docker images -f "dangling=true" -q)

